package cn.com.woxitech.dev.controller;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.JsonNode;

import cn.com.woxitech.dev.common.APIResult;
import cn.com.woxitech.dev.common.APIResultFactory;
import cn.com.woxitech.dev.common.Const;
import cn.com.woxitech.dev.common.JSONUtils;
import cn.com.woxitech.dev.model.WXSDKConfig;
import cn.com.woxitech.dev.model.gen.WmUser;
import cn.com.woxitech.dev.service.UserService;
import cn.com.woxitech.dev.service.WeixinService;

@RestController
public class WeixinAPIController {
	
	@Autowired 
	private WeixinService weixinService;
	
	@Autowired
	private UserService userService;
	

	@RequestMapping(value = "/weixin/pay_orderinfo", method = RequestMethod.POST)
	public APIResult pay_orderinfo(HttpServletResponse response,  @RequestBody String body) {
		try {
			JsonNode jsonNode = JSONUtils.toJson(body);
			String appid = jsonNode.get("appid").asText();
			String systemType = jsonNode.get("systemType").asText();
			String unionid = jsonNode.get("unionid").asText();
			String roomuuid = jsonNode.get("roomuuid").asText();
			double fee = jsonNode.get("fee").asDouble();
			
			
			WmUser user = this.userService.getUserByUnionId(unionid);
			
			Map<String, String> mp = new HashMap<String, String>();
			
			long t = System.currentTimeMillis();
			String des = "捐献给直播上科大App";
			String tradeNo = "jz_"+t;
			String attach = unionid + "_"+roomuuid + "_" + fee;
			String feeType = "CNY";
			SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmss"); 
			String timeStart = formatter.format(new Date(t)); 
			
			t = t + 1000 * 60 * 10; //10分钟
			
			String timeExpire = formatter.format(new Date(t)); 
			
			mp.put("apikey", Const.ZBSKD_OPENAPI_APPID);
			mp.put("mchId", Const.ZBSKD_MCH_ID);
			mp.put("partnerKey", Const.ZBSKD_MCH_SECRETKEY);
			mp.put("des", des);
			mp.put("tradeNo", tradeNo);
			mp.put("detail", des);
			mp.put("attach",attach);
			mp.put("feeType",feeType);
			mp.put("timeStart",timeStart);
			mp.put("timeExpire",timeExpire);
			mp.put("productId",Const.ZBSKD_PRODUCT1_ID);
//			mp.put("openId", user.getOpenOpenid());
			
			
			return APIResultFactory.success(mp);				

		}catch(Exception e){
			e.printStackTrace();
			return APIResultFactory.fail();
		}
	}
	
	@RequestMapping(value = "/weixin/pay_orderinfo_haojs", method = RequestMethod.POST)
	public APIResult pay_orderinfo_haojs(HttpServletResponse response,  @RequestBody String body) {
		try {
			JsonNode jsonNode = JSONUtils.toJson(body);
			String appid = jsonNode.get("appid").asText();
			String systemType = jsonNode.get("systemType").asText();
			String unionid = jsonNode.get("unionid").asText();
			String roomuuid = jsonNode.get("roomuuid").asText();
			double fee = jsonNode.get("fee").asDouble();
			
			
			WmUser user = this.userService.getUserByUnionId(unionid);
			
			Map<String, String> mp = new HashMap<String, String>();
			
			long t = System.currentTimeMillis();
			String des = "捐献给好教授在线";
			String tradeNo = "jz_"+t;
			String attach = unionid + "_"+roomuuid + "_" + fee;
			String feeType = "CNY";
			SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmss"); 
			String timeStart = formatter.format(new Date(t)); 
			
			t = t + 1000 * 60 * 10; //10分钟
			
			String timeExpire = formatter.format(new Date(t)); 
			
			mp.put("apikey", Const.HJSZX_OPENAPI_APPID);
			mp.put("mchId", Const.HJSZX_MCH_ID);
			mp.put("partnerKey", Const.HJSZX_MCH_SECRETKEY);
			mp.put("des", des);
			mp.put("tradeNo", tradeNo);
			mp.put("detail", des);
			mp.put("attach",attach);
			mp.put("feeType",feeType);
			mp.put("timeStart",timeStart);
			mp.put("timeExpire",timeExpire);
			mp.put("productId",Const.ZBSKD_PRODUCT1_ID);
//			mp.put("openId", user.getOpenOpenid());
			
			
			return APIResultFactory.success(mp);				

		}catch(Exception e){
			e.printStackTrace();
			return APIResultFactory.fail();
		}
	}
	
	@RequestMapping(value = "/weixin/wxsdk_config", method = RequestMethod.POST)
	public APIResult wxsdk_config(HttpServletResponse response,  @RequestBody String body) {
		try {
			
			JsonNode _body = JSONUtils.toJson(body);
			String calledUrl  = _body.get("url").textValue();
		
			WXSDKConfig wxSDKConfig = this.weixinService.getWeiXinSDKConfig(calledUrl);
			
			if(wxSDKConfig == null){
				return APIResultFactory.fail();
			}else{
				return APIResultFactory.success(wxSDKConfig);				
			}

		}catch(Exception e){
			e.printStackTrace();
			return APIResultFactory.fail();
		}
	}
	
	
	@RequestMapping(value = "/weixin/access_token", method = RequestMethod.GET)
	public APIResult access_token(HttpServletResponse response) {
		
		try {
			String access_token = this.weixinService.getAccessToken();
			
			if(access_token == null){
				return APIResultFactory.fail();
			}else{
				return APIResultFactory.success(access_token);	
			}
			
		}catch(Exception e){
			return APIResultFactory.fail();
		}
	}
	
	@RequestMapping(value = "/weixin/jsapi_ticket", method = RequestMethod.GET)
	public APIResult jsapi_ticket(HttpServletResponse response) {
		
		try {
			String jsapi_ticket = this.weixinService.getJsApiTicket();
			
			if(jsapi_ticket == null){
				return APIResultFactory.fail();
			}else{
				return APIResultFactory.success(jsapi_ticket);	
			}
			
		}catch(Exception e){
			return APIResultFactory.fail();
		}
	}
	
	
	@RequestMapping(value = "/weixin/config", method = RequestMethod.GET)
	public APIResult weixin_config(HttpServletResponse response) {
		
		Map<String, Object> mp = new HashMap<String, Object>();
			
		mp.put("appid", Const.HJSZX_OPENAPI_APPID);
		mp.put("redirect_uri", Const.HJSZX_PRODUCTION_SERVER_BASE + "/mvc/qrcodeLogin_callback");
		mp.put("state","login");

		return APIResultFactory.success(mp);	
	}
	
}
